﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace sdamarketing 
{
    public partial class Registracija : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            

            if (!IsPostBack)
            {
                Session.Abandon();
                PanelInteresi.Visible = false;
                DataSet Sex = ReturnDataSet.ReturSet("SELECT idSex, sex FROM Sex");
                DropDownListSex.DataValueField = "idSex";
                DropDownListSex.DataTextField = "sex";
                DropDownListSex.DataSource = Sex;
                DropDownListSex.DataBind();
                DropDownListSex.Items.Insert(0, new ListItem("", ""));

                DataSet Age = ReturnDataSet.ReturSet("SELECT idAge, fromAndTo FROM Age");
                DropDownListAge.DataValueField = "idAge";
                DropDownListAge.DataTextField = "fromAndTo";
                DropDownListAge.DataSource = Age;
                DropDownListAge.DataBind();
                DropDownListAge.Items.Insert(0, new ListItem("", ""));

                DataSet Education = ReturnDataSet.ReturSet("SELECT idEducation, education FROM Educations");
                DropDownListEducation.DataValueField = "idEducation";
                DropDownListEducation.DataTextField = "education";
                DropDownListEducation.DataSource = Education;
                DropDownListEducation.DataBind();
                DropDownListEducation.Items.Insert(0, new ListItem("", ""));

                DataSet Status = ReturnDataSet.ReturSet("SELECT idSocialStructure, status FROM SocialStructures");
                DropDownListStructure.DataValueField = "idSocialStructure";
                DropDownListStructure.DataTextField = "status";
                DropDownListStructure.DataSource = Status;
                DropDownListStructure.DataBind();
                DropDownListStructure.Items.Insert(0, new ListItem("", ""));

                DataSet Nacionalost = ReturnDataSet.ReturSet("SELECT idNacionalnost,Nacionalnost FROM Nationality");
                DropDownListNationality.DataValueField = "idNacionalnost";
                DropDownListNationality.DataTextField = "Nacionalnost";
                DropDownListNationality.DataSource = Nacionalost;
                DropDownListNationality.DataBind();
                DropDownListNationality.Items.Insert(0, new ListItem("", ""));

                DataSet Regions = ReturnDataSet.ReturSet("SELECT idRegion,region FROM Regions");
                DropDownListRegion.DataValueField = "idRegion";
                DropDownListRegion.DataTextField = "region";
                DropDownListRegion.DataSource = Regions;
                DropDownListRegion.DataBind();
                DropDownListRegion.Items.Insert(0, new ListItem("", ""));

                DataSet Locality = ReturnDataSet.ReturSet("SELECT idLocality,locality FROM Localitys");
                DropDownListLocality.DataValueField = "idLocality";
                DropDownListLocality.DataTextField = "locality";
                DropDownListLocality.DataSource = Locality;
                DropDownListLocality.DataBind();
                DropDownListLocality.Items.Insert(0, new ListItem("", ""));
            }
        }

        protected void saveButton_Click(object sender, EventArgs e)
        {
            
            try
            {
                if (Users.FindUser(UsernameTextBox.Text.ToString()) == 0)
                {
                    
                    Users.AddUser(FirstNameTextBox.Text, LastNameTextBox.Text, UsernameTextBox.Text, PasswordTextBox.Text, EMailTextBox.Text, QuestionTextBox.Text, AnswerTextBox.Text, 2, Int32.Parse(DropDownListSex.SelectedValue.ToString()), Int32.Parse(DropDownListEducation.SelectedValue.ToString()), Int32.Parse(DropDownListStructure.SelectedValue.ToString()), Int32.Parse(DropDownListAge.SelectedValue.ToString()), Int32.Parse(DropDownListNationality.SelectedValue.ToString()),Int32.Parse(DropDownListRegion.SelectedValue.ToString()),Int32.Parse(DropDownListLocality.SelectedValue.ToString()));
                    
                    lblMessage.Text = "Корисникот е успешно додаден";

                    // Vnesuvanje Informacii za korisnikot vo sesija
                    DataSet dataSet = Users.authenticateUser(UsernameTextBox.Text, PasswordTextBox.Text);
                    // proveruvam dali e blokiran ili izbrisan userot (ima edno flagce vo baza isBlock)
                    if (dataSet.Tables["User"].Rows[0].ItemArray[1].ToString() == "False" && dataSet.Tables["User"].Rows[0].ItemArray[2].ToString() == "False")
                    {
                        Session["idUser"] = dataSet.Tables["User"].Rows[0].ItemArray[0].ToString();
                        Session["username"] = FirstNameTextBox.Text;
                        Session["idRole"] = dataSet.Tables["User"].Rows[0].ItemArray[3].ToString();
                    }
                    PanelRegistracja.Visible = false;
                }
                else
                {
                   lblMessage.Text = "Корисникот со такво корисничко име веќе постои";
                }
                DropDownListSex.ClearSelection();
                DropDownListAge.ClearSelection();
                DropDownListEducation.ClearSelection();
                DropDownListStructure.ClearSelection();
                DropDownListNationality.ClearSelection();
                DropDownListLocality.ClearSelection();
                DropDownListRegion.ClearSelection();
                PanelInteresi.Visible = Visible;
                FirstNameTextBox.Text = "";     FirstNameTextBox.Enabled = false;
                LastNameTextBox.Text = "";      LastNameTextBox.Enabled = false;
                UsernameTextBox.Text = "";      UsernameTextBox.Enabled = false;
                EMailTextBox.Text = "";         EMailTextBox.Enabled = false;
                QuestionTextBox.Text = "";      QuestionTextBox.Enabled = false;
                AnswerTextBox.Text = "";        AnswerTextBox.Enabled = false;
                PasswordTextBox.Text = "";      PasswordTextBox.Enabled = false;
                PasswordConfTextBox.Text = "";  PasswordConfTextBox.Enabled = false;

            }
            catch (SqlException exception)
            {
                lblMessage.Text = "Грешка обидетесе повторно";
                SendToLogFile.SendRecordToFIle(DateTime.Now.ToString() + " -exc- " + exception.Message.ToString() + " -stackTrace- " + exception.StackTrace.ToString() );
            }
        }

        protected void btnInteres_Click(object sender, EventArgs e)
        {

            try
            {
                int ajdiNaUser = Int32.Parse(Session["idUser"].ToString());

                Zainteresiranost.AddInteresi(1, Int32.Parse(avtoslider.Text), ajdiNaUser);
                Zainteresiranost.AddInteresi(2, Int32.Parse(oruzjeslider.Text), ajdiNaUser);
                Zainteresiranost.AddInteresi(3, Int32.Parse(computerslider.Text), ajdiNaUser);
                Zainteresiranost.AddInteresi(4, Int32.Parse(foodslider.Text), ajdiNaUser);
                Zainteresiranost.AddInteresi(5, Int32.Parse(drinksslider.Text), ajdiNaUser);
                Zainteresiranost.AddInteresi(6, Int32.Parse(electronicsslider.Text), ajdiNaUser);
                Zainteresiranost.AddInteresi(7, Int32.Parse(fashionslider.Text), ajdiNaUser);
                Zainteresiranost.AddInteresi(8, Int32.Parse(phoneslider.Text), ajdiNaUser);
                Zainteresiranost.AddInteresi(9, Int32.Parse(musicslider.Text), ajdiNaUser);
                Zainteresiranost.AddInteresi(10, Int32.Parse(moviesslider.Text), ajdiNaUser);

                lblTest.Text = "Успешно се регистрирани вашите интереси.";
                Response.Redirect("Account/Home.aspx");
            }
            catch (SqlException exception)
            {
                lblTest.Text = "Грешка обидетесе повторно";
                SendToLogFile.SendRecordToFIle(DateTime.Now.ToString() + " -exc- " + exception.Message.ToString() + " -stackTrace- " + exception.StackTrace.ToString() );
            }
        }
    }
}
